<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use session;
class goodsController extends Controller
{   
	//前台商品分类
    public function sort()
    {
     $data = DB::table('sort')	-> where('pid','1') -> get();
     //dd($data);
     $goods = DB::table('goods') -> get();
     //dd($goods);
      return view('home.goods.sort',['data' => $data],['goods' => $goods]);
    }
    //前台商品详细信息
    public function goods($id)
    {
    	$data = DB::table('goods as g')
    	     -> select('g.*','s.name as sname','t.*','g.id as ggid')
    	     -> LeftJoin('sort as s','s.id','=','g.sid')
    	     -> leftjoin('type as t','g.id','=','t.gid')
    	     -> where('g.id',$id)
    	     -> first();
    	
            $data -> content = strip_tags($data -> content);
       
    	$type = DB::table('type') -> where('gid',$id) -> get();
    	//dd($data);
    	return view('home.goods.goods',['data' => $data],['type' => $type]);
    }
    public function sorts($id)
    {
    	$data = DB::table('sort') -> where('pid',1) -> get();
    	$goods = DB::table('goods') -> where('sid',$id) -> get();
    	// dd($goods);
    	return view('home.goods.sort',['data' => $data],['goods' => $goods]);
    }

    public function sortss($sid)
    {   

    	$goods = DB::table('sort as s1')
    	      -> select('s1.id','s2.name')
    	      -> leftjoin('sort as s2','s2.pid','=','s1.id')
    	      -> where('s1.pid',$sid)
    	      -> get();
         // dd($goods);
         $good = DB::table('sort') -> where('pid',0) -> get();
         $sort = DB::table('sort') -> where('id',$sid) -> first();
         //dd($sort);
         //dd($name);
         $res = array();
         foreach($goods as $id)
         {
         	$oid = $id -> id;
            $res[] = DB::table('goods')
                  -> where('sid',$oid)
                  -> get();
        
         }	        
         if($sid == '2')
         {
       	  $res[] = DB::table('goods') -> where('sid',2) -> get();

         }
       
         if($sid == '0')
         {   
         	 $res ='';
         	$res[]= DB::table('goods') -> get();
         } 
          // dd($sort);
         return view('home.goods.sortss',['res' => $res,'sort' => $sort],['good' => $good]);	
    	     
    }
     
   
}
